for($chr=23;$chr<24;$chr++)
{
	print "dealing with chr $chr\n";
	#opening the files
	if($chr != 23)
	{
		$file= "ALL_1000G_phase1integrated_feb2012_chr".$chr."_impute.legend.gz";
		open(LEGEND,"gunzip -c /data4/bsi/refdata/genetics/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRLEGEND,"| gzip -c > ./impute_ref_legend/$file") or die "unable to write $file\n";
		$file= "ALL_1000G_phase1integrated_feb2012_chr".$chr."_impute.hap.gz";
		open(HAP,"gunzip -c /data4/bsi/refdata/genetics/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		#open(WRHAP,"| gzip -c > ./impute_ref_legend/$file") or die "unable to write $file\n";
	}
	else
	{
		$file = "ALL_1000G_phase1integrated_feb2012_chrX_nonPAR_impute.legend.gz";
		open(LEGEND,"gunzip -c /data4/bsi/refdata/genetics/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		open(WRLEGEND,"| gzip -c > ./impute_ref_legend/$file") or die "unable to write $file\n";
		$file= "ALL_1000G_phase1integrated_feb2012_chrX_nonPAR_impute.hap.gz";
		open(HAP,"gunzip -c /data4/bsi/refdata/genetics/impute/ALL_1000G_phase1integrated_feb2012_impute/$file |") or die " no file found $file \n";
		#open(WRHAP,"| gzip -c > ./impute_ref_legend/$file") or die "unable to write $file\n";
	}
	$line = <LEGEND>;
	chomp($line);
	print WRLEGEND $line." all.maf\n";
	while(<LEGEND>)
	{	chomp($_);
		$line = <HAP>;
		@legend = split(" ",$_);
		$count_1 =($line =~ tr/1//);
		$count_0 = ($line =~ tr/0//);
		if($chr == 23)
		{
			$count_d = ($line =~ tr/-//);
			if($count_1+$count_0+$count_d != 2184)
			{
				die "count_1 $count_1 count_0 $count_0 count_d $count_d\n $line";
			}
		}
		else
		{
			if($count_1+$count_0 != 2184)
                        {
                                die "count_1 $count_1 count_0 $count_0  $line\n";
                        }
		}	
			#print "count_1 $count_1 count_0 $count_0\n";
			$percent_0 = $count_0/($count_1+$count_0);
			$percent_1 = $count_1/($count_1+$count_0);
			#if($percent_0 > 0.005 && $percent_1 > 0.005 && $legend[4] eq "SNP")
			#{
			#	print WRLEGEND $_;
			#	print WRHAP $line;
			#}
			if($percent_0 <= $percent_1)
			{
				print WRLEGEND $_." $percent_0\n";
			}
			else
			{
				print WRLEGEND $_." $percent_1\n";
			}
			
	}

}
